package com.enlorenz.bike;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.enlorenz.common.util.CommonContants;
import com.enlorenz.common.util.DataDetUtils;
import com.enlorenz.common.util.FileUtils;
import com.enlorenz.common.util.NumberUtils;
import com.enlorenz.common.util.TimeConstants;
import com.enlorenz.common.util.TimeUtils;


public class ParseFitLog extends DefaultHandler{
	private List<FitLog> fitlogList = null;
	private FitLog fitlog = null;
	private String preTag = null;//作用是记录解析时的上一个节点名称
	
	public static void main(String[] args) throws FileNotFoundException, Exception {
		int lastSec=-5;
		
		List<FitLog> list=new ParseFitLog().getFitLog(new FileInputStream("e:\\ban.fitlog"));
		for(FitLog ft:list){
			Integer time=Integer.valueOf(ft.getTm())+lastSec;
			ft.setTm(String.valueOf(time));
		}
		
		//ReflectionUtils.print(list);
		StringBuilder msg=new StringBuilder();
		int index=1;
		FitLog last=null;
		for(FitLog ft:list){
			if(Integer.valueOf(ft.getTm())<0){
				continue;
			}
			
			msg.append(index);
			msg.append(CommonContants.WRAP_SYMBOL);
			
			Integer costTime=0;
			Double distance=0.0;
			Double height=0.0;
			Double line=0.0;
			if(null == last){
				msg.append("00:00:00,000 --> 00:00:00,000");
			}else{
				String lastTime=TimeUtils.
						parseTimeElementAndForamt("HH:mm:ss", 
						Integer.valueOf(last.getTm())*TimeConstants.SECOND_MILLI);
				String nowTime=TimeUtils.
						parseTimeElementAndForamt("HH:mm:ss", 
						Integer.valueOf(ft.getTm())*TimeConstants.SECOND_MILLI);
				
				msg.append(lastTime+",000 --> "+nowTime+",000");
				
				costTime=Integer.valueOf(ft.getTm())-Integer.valueOf(last.getTm());
				distance=Double.valueOf(ft.getDist())-Double.valueOf(last.getDist());
				height=Double.valueOf(ft.getEle())-Double.valueOf(last.getEle());
				
				if(0 != distance){
					line=NumberUtils.formatDecimal((height/distance)*100,3);
				}
			}
			msg.append(CommonContants.WRAP_SYMBOL);
			
			Double speed=0.0;
			if(0 != costTime){
				speed=(distance/costTime)*3.6;
				speed=NumberUtils.
						formatWithTwoDecimal(speed);
			}
			
			String heart=ft.getHr();
			if(DataDetUtils.isStrEmpty(heart)){
				heart="?";
			}
			
			msg.append("速度:");
			msg.append(speed);
			msg.append("KM/H 心率:");
			msg.append(heart);
			msg.append("bpm 坡度:");
			msg.append(line);
			msg.append("% 距离:");
			msg.append(ft.getDist());
			msg.append("M");
			msg.append(CommonContants.WRAP_SYMBOL);
			msg.append(CommonContants.WRAP_SYMBOL);
			
			index++;
			last=ft;
		}
		System.out.println(msg);
		
		FileUtils.writeFile("E:\\test.srt",
				msg.toString(), "gbk");
	}
	
	public List<FitLog> getFitLog(InputStream xmlStream) throws Exception{
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		ParseFitLog handler = new ParseFitLog();
		parser.parse(xmlStream, handler);
		return handler.getBooks();
	}
	
	public List<FitLog> getBooks(){
		return fitlogList;
	}
	
	@Override
	public void startDocument() throws SAXException {
		fitlogList = new ArrayList<FitLog>();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if("pt".equals(qName)){
			fitlog = new FitLog();
			fitlog.setTm(attributes.getValue("tm"));
			fitlog.setLat(attributes.getValue("lat"));
			fitlog.setLon(attributes.getValue("lon"));
			fitlog.setEle(attributes.getValue("ele"));
			fitlog.setDist(attributes.getValue("dist"));
			fitlog.setHr(attributes.getValue("hr"));
			fitlog.setCadence(attributes.getValue("cadence"));
			
		}
		preTag = qName;//将正在解析的节点名称赋给preTag
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if("pt".equals(qName)){
			fitlogList.add(fitlog);
			fitlog = null;
		}
		preTag = null;/**当解析结束时置为空。这里很重要，例如，当图中画3的位置结束后，会调用这个方法
		，如果这里不把preTag置为null，根据startElement(....)方法，preTag的值还是book，当文档顺序读到图
		中标记4的位置时，会执行characters(char[] ch, int start, int length)这个方法，而characters(....)方
		法判断preTag!=null，会执行if判断的代码，这样就会把空值赋值给book，这不是我们想要的。*/
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		
	}
	
}